<?php
namespace app\ausadmin\validate;
use think\Validate;
use think\Db;
class Operator extends Validate
{

    protected $rule=[
        'name'=>'require|checkName',
        'identifying' => 'require|checkUnique',
        'contacts'=>'require',
        'mobile'=>'require',
        'version'=>'require',
        'expiration_date'=>'require',

    ];

    protected $message=[
        'name.require'=>'运营商名称不得为空！',
        'name.checkName'=>'运营商名称不得重复！',
        'identifying.require'=>'企业标识不能为空',
        'identifying.checkUnique'=>'企业标识名称不能重复',
        'contacts.require'=>'联系人不得为空！',
        'mobile.require'=>'联系电话不得为空！',
        'version.require'=>'版本不能为空',
        'expiration_date.require'=>'到期时间不能为空',

    ];

    protected function checkName($value,$rule,$data){
        $checkWhere = [
            'name'=>$value,
            'is_delete'=> 1
        ];
        $res = Db::name('operator')->where($checkWhere)->find();
        return !empty($res) ? false:true;
    }
    protected function checkUnique($value,$rule,$data){
        $checkWhere = [
            'identifying'=>$value,
            'is_delete'=> 1
        ];
        $res = Db::name('operator')->where($checkWhere)->find();
        return !empty($res) ? false:true;
    }

    protected $scene=[
        'add'=>['name','contacts','mobile','identifying','version','expiration_date'],
        'edit'=>['name','contacts','mobile','identifying','version','expiration_date'],
    ];
}